<?php
class BancoDeDados {

	private $con;
	private $conexao;

	//TODO Externalizar as confs do BD para um .ini
	function getSeqIdByMd5($md5s) {

		$query = "SELECT id_seq FROM en_seq  WHERE md5 in ('implode('\',\'',$md5s)')";
		$resp = mysql_query($query, $this->conexao);
		$array = array();
		while ( ( $row = mysql_fetch_array($resp) ) !== false )
		{
			$array[] = $row;
		}
		return $array;

	}

	function getSeqDb($id_anal, $ids_seq_in, $max_num_seq_db) {
		//TODO Arrumar isso!!!!!
		if (count($ids_seq_in) == 0) {
			$and = '';
		} else {
			$and = "and seq_in IN (".implode(',',$ids_seq_in).")";
		}

		$query = "SELECT seq_db FROM compartion_cache  WHERE  id_job = 0 $and LIMIT $max_num_seq_db";
		$resp = mysql_query($query, $this->conexao);
		$array = array();
		while ( ( $row = mysql_fetch_array($resp) ) !== false )
		{
			$array[] = $row;
		}
		return $array;

	}

function setJobDone($job_id,$status){
		$query = "UPDATE job SET done = $status WHERE id_job ='$job_id'";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);
		$num = mysql_fetch_row($resp);
		
		if($num[0] == 1){
			return true;
		}else{
			return false;
		}
		
	}
function getSeqById($seq_ids){
		$query = "SELECT seq FROM en_seq  WHERE id_seq IN (implode(',',$seq_ids))";
		$resp = mysql_query($query, $this->conexao);
		$array = array();
		while ( ( $row = mysql_fetch_array($resp) ) !== false )
		{
			$array[] = $row;
		}
		return $array;

		
	}
function BancoDeDados($main) {
		if ($main) {
			$this->conexao = mysql_connect("r2d2.ger.ufsc.br", "panic", "p4n1c");
			$this->con = mysql_select_db("panic", $this->conexao) or die(mysql_error());

		} else {
			$this->conexao = mysql_connect("r2d2.ger.ufsc.br", "panic", "p4n1c");
			$this->con = mysql_select_db("panic", $this->conexao) or die(mysql_error());
		}

	}
function realocate($mac) {
		//$query = "UPDATE resources SET id_analysis = (select id_analysis from analysis order by priority desc limit 1) where machine_id_mac = '$mac'";
		$query = "DELETE FROM resources where machine_id_mac in (select id_mac from machine where mac='$mac')";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);

		$query = "INSERT INTO resources VALUES((select id_analysis from analysis order by priority desc limit 1),(select id_mac from machine where mac='$mac'))";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);

		$query = "select id_analysis from resources WHERE machine_id_mac in (select id_mac from machine where mac='$mac' LIMIT 1) LIMIT 1";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);

		$row = mysql_fetch_row($resp);
		return $row[0];
	}
function macWorkingFor($mac, $id_anal) {
		$query = "SELECT count(*) FROM resources  WHERE id_analysis = '$id_anal' and machine_id_mac in (select id_mac from machine where mac='$mac' LIMIT 1) LIMIT 1";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);
		$num = mysql_fetch_row($resp);

		return $num[0];
	}
function getIdAnalByMac($mac) {
		$query = "SELECT id_analysis FROM resources  WHERE machine_id_mac in (select id_mac from machine where mac='$mac' LIMIT 1) LIMIT 1";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);
		$num = mysql_fetch_row($resp);

		if (count($num) == 0) {
			return 0;
		} else {
			return $num[0];
		}
	}
function isDone($id_anal) {
		$query = "SELECT done FROM  analysis WHERE id_analysis = $id_anal LIMIT 1";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);
		$num = mysql_fetch_row($resp);

		return $num[0];
	}
function existMac($mac) {
		$query = "SELECT count(*) FROM machine  WHERE mac = '$mac' LIMIT 1";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);
		$num = mysql_fetch_row($resp);

		if ($num[0] == 0) {
			return false;
		} else {
			return true;
		}
	}
function getMacIdByMac($mac) {
		$query = "SELECT id_mac FROM machine  WHERE mac = '$mac' LIMIT 1";
		$resp = mysql_query($query, $this->conexao);
		$this->mysqlErrorDealer($resp, $query);
		$num = mysql_fetch_row($resp);
		return $num[0];
	}
function createJob($id_mac,$id_anal,$old_seq_db_ids,$new_seq_db_ids,$old_seq_in_ids,$new_seq_in_ids) {
	
	//TODO Rever Essa Bagaça aqui.
	
	$query = "INSERT INTO job VALUES(null,$id_mac,now(),true,false,$id_anal)";
	$resp = mysql_query($query, $this->conexao);
	$this->mysqlErrorDealer($resp, $query);
	$id_job = mysql_insert_id();
	
	
	
	if((count($old_seq_db_ids)==0)or(count($new_seq_db_ids)==0)){
		$delimiterDB = '';
	}else{
		$delimiterDB = ',';
	}
	
	if((count($old_seq_in_ids)==0)or(count($new_seq_in_ids)==0)){
		$delimiterIN = '';
	}else{
		$delimiterIN = ',';
	}
	
	$query = "UPDATE comparation_cache SET id_job = $id_job " .
			"WHERE seq_in IN (implode(',',$old_seq_in_ids).$delimiterDB.implode(',',$new_seq_in_ids)) " .
			"  and seq_db IN (implode(',',$old_seq_db_ids).$delimiterDB.implode(',',$new_seq_db_ids))";
	$resp = mysql_query($query, $this->conexao);
	$this->mysqlErrorDealer($resp, $query);
	
	return $id_job;
}
function insertMacOnMachine($mac, $mail, $so = 'Unknow', $ram = '0', $procs = '1') {
	$query = "INSERT INTO machine VALUES(null,(select id_user from users where mail = '$mail'), $ram,$procs,'$so','$mac')";
	$resp = mysql_query($query, $this->conexao);
	$this->mysqlErrorDealer($resp, $query);

}
function updateMac($mac, $mail) {
	$query = "UPDATE machine SET id_user = (select id_user from users where mail = '$mail') WHERE mac='$mac'";
	$resp = mysql_query($query, $this->conexao);
	$this->mysqlErrorDealer($resp, $query);
}
function onBlackList($mac) {
	$query = "SELECT count(*) FROM blacklist  WHERE id_MAC in (select id_mac from machine where mac='$mac' LIMIT 1) LIMIT 1";
	$resp = mysql_query($query, $this->conexao);
	$this->mysqlErrorDealer($resp, $query);
	$num = mysql_fetch_row($resp);
	if ($num[0] == 0) {
		return false;
	} else {
		return true;
	}
}
function existAccount($mail) {
	$query = "SELECT count(*) FROM users  WHERE mail = '$mail' LIMIT 1";
	$resp = mysql_query($query, $this->conexao);
	$this->mysqlErrorDealer($resp, $query);
	$num = mysql_fetch_row($resp);
	if ($num[0] == 0) {
		return false;
	} else {
		return true;
	}
}
 function mysqlErrorDealer($resp, $query) {
	if ($resp == FALSE) {
		syslog(LOG_SYSLOG, "Panic@Home | " . date('Y-m-d\TH:i:s\Z') . " | Error =>  " . mysql_error($this->conexao) . ' | Query => ' . $query);
	}

}

}

abstract class banco {
	private $dbmain;
	private $dbcache;

	function getInstanceDBMain() {
		if (!isset ($dbmain)) {
			$dbmain = new BancoDeDados(true);
		}
		return $dbmain;
	}
	function getInstanceDBCache() {
		if (!isset ($dbcache)) {
			$dbcache = new BancoDeDados(false);
		}
		return $dbcache;
	}
}
?>